#include <LiquidCrystal.h>
#include <SoftwareSerial.h>
SoftwareSerial ms(10, 7);
int zuojia = 44;
int zuojian = 40;
int sure = 36;
int youjia = 66;
int youjian = 32;
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 31, en = 27, d4 = 23, d5 = 19, d6 = 15, d7 = 11;
LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
unsigned long timer = 0;
String req = "", LDT, RDT, Lt, Rt, flag;

void setup() {
  Serial.begin(9600);
  ms.begin(9600);
  pinMode(zuojia, INPUT_PULLUP);
  pinMode(zuojian, INPUT_PULLUP);
  pinMode(sure, INPUT_PULLUP);
  pinMode(youjia, INPUT_PULLUP);
  pinMode(youjian, INPUT_PULLUP);
  pinMode(13, OUTPUT);
  digitalWrite(13, LOW);
  // set up the LCD's number of columns and rows:
  lcd.begin(16, 2);
  // Print a message to the LCD.
  lcd.print("wait camera IP.");
  delay(2000);
  while (1) {
    while (ms.available() > 0) {
      for (int  thisRow = 0; thisRow < 2; thisRow++) {
        // loop over the rows:
        for (int thisCol = 0; thisCol < 16; thisCol++) {
          // set the cursor position:
          lcd.setCursor(thisCol, thisRow);
          // print the letter:
          char c =  char(ms.read());
          lcd.write(c);
          delay(30);
          if (!digitalRead(sure)) {
            ms.write(0xf1);
            break;
          }
        }
      }
    }
    if (!digitalRead(sure)) {
      ms.write(0xf1);
      break;
    }
  }
  lcd.clear();
  lcd.print(" <--WELCOME--> ");
  while (!digitalRead(sure));
  delay(1000);
}

void loop() {
  button(zuojia);
  button(zuojian);
  button(sure);
  button(youjia);
  button(youjian);
  if ((millis() - timer) > 2000) {
    lcd.clear();
    ms.write(0xf7);
    timer = millis();
  }
  req = "";
  if (ms.available()) {
    delay(100);
    while (ms.available()) {
      req += char(ms.read());
    }
    //    Serial.println(req);
    if ((req.startsWith("A")) && (req.endsWith("Z"))) {
      byte loc = 0, index;
      for (int i = 0; i < 5; i++) {
        index = req.indexOf(",", loc + 1);
        switch (i) {
          case 0:
            LDT = req.substring(loc + 1, index);
            break;
          case 1:
            RDT = req.substring(loc + 1, index);
            break;
          case 2:
            Lt = req.substring(loc + 1, index);
            break;
          case 3:
            Rt = req.substring(loc + 1, index);
            break;
          case 4:
            flag = req.substring(loc + 1, index);
            break;
          default:
            break;
        }
        loc = index;
      }
    }
    //    Serial.println(String("") + LDT + "\t" + RDT + "\t" + Lt + "\t" + Rt + "\t" + flag);
  }
  lcd.setCursor(0, 0);
  lcd.print("LDT:");
  lcd.setCursor(4, 0);
  lcd.print(LDT);
  lcd.setCursor(8, 0);
  lcd.print("RDT:");
  lcd.setCursor(12, 0);
  lcd.print(RDT);
  lcd.setCursor(0, 1);
  lcd.print("Lt:");
  lcd.setCursor(3, 1);
  lcd.print(Lt);
  lcd.setCursor(8, 1);
  lcd.print("Rt:");
  lcd.setCursor(11, 1);
  lcd.print(Rt);
  if (flag.toInt())
    digitalWrite(13, HIGH);
  else
    digitalWrite(13, LOW);
}

void button(int pin) {
  if (!digitalRead(pin)) {
    delay(100);
    if (!digitalRead(pin)) {
      switch (pin) {
        case 44:
          ms.write(0xf2);
          break;
        case 40:
          ms.write(0xf3);
          break;
        case 36:
          ms.write(0xf4);
          break;
        case 66:
          ms.write(0xf5);
          break;
        case 32:
          ms.write(0xf6);
          break;
        default:
          break;
      }
    }
  }
}
